package com.epfresh.supplier.activity;


import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.epfresh.api.dialog.TimePickerDialog;
import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.RequestEntityMapSupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.entity.StatusEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.ucrop.PhotoCore;
import com.epfresh.api.utils.CashierInputFilter;
import com.epfresh.api.utils.DateUtil;
import com.epfresh.api.utils.VerificationUtils;
import com.epfresh.api.widget.CommonInputItem;
import com.epfresh.supplier.R;
import com.epfresh.supplier.constant.Constant;
import com.epfresh.supplier.delegate.UploadProductDelegate;
import com.epfresh.supplier.entity.PurchaseInfo;
import com.epfresh.supplier.entity.StoreInfo;
import com.google.gson.Gson;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class StoreMangerInfoActivity extends BaseActivity {


    private CommonInputItem ciManagerName;
    private CommonInputItem ciManagerPhone;

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("managerName", getIntent().getStringExtra("managerName"));
        outState.putString("managerPhone", getIntent().getStringExtra("managerPhone"));
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_manager_info);
        if (savedInstanceState != null) {
            getIntent().putExtra("managerName", savedInstanceState.getString("managerName"));
            getIntent().putExtra("managerPhone", savedInstanceState.getString("managerPhone"));
        }
        ivBack.setVisibility(View.VISIBLE);
        tvBack.setVisibility(View.VISIBLE);
        toolbarTitle.setText("运营人信息");
        rightbtn.setText("保存");
        rightbtn.setTextColor(0xff898989);
        rightbtn.setVisibility(View.VISIBLE);
//        rightbtn.setOnClickListener(this);

        initView();

    }

    @Override
    public void onRightClick() {
        save();
    }

    private void initView() {

        ciManagerName = (CommonInputItem) findViewById(R.id.ci_manager_name);
        ciManagerPhone = (CommonInputItem) findViewById(R.id.ci_manager_phone);

        ciManagerName.setContentText(getIntent().getStringExtra("managerName"));
        ciManagerPhone.setContentText(getIntent().getStringExtra("managerPhone"));
        ciManagerName.setSelectionLast();
    }


    @Override
    public void onClick(View view) {
        super.onClick(view);


    }


    private void save() {

        String managerName = ciManagerName.getContentText();
        String managerPhone = ciManagerPhone.getContentText();

        if (managerName == null || managerName.length() < 1) {

            toast("请输入运营人姓名");
            return;
        }

        if (!VerificationUtils.isValid(managerName)) {
            toast("请输入合法的内容");
            return;
        }

        if (managerPhone == null || managerPhone.length() < 1) {

            toast("请输入运营人电话");
            return;
        }

        requestModifyMangerInfo(managerName, managerPhone);
    }


    private void requestModifyMangerInfo(final String managerName, final String managerPhone) {

        RequestEntityMapSupply RequestEntityMapSupply = new RequestEntityMapSupply();
        RequestEntityMapSupply.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_store_managerInfo_modify);

        RequestEntityMapSupply.putParameter("managerName", managerName);
        RequestEntityMapSupply.putParameter("managerPhone", managerPhone);
        request(RequestEntityMapSupply, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_store_managerInfo_modify, new OnRequestListener<StatusEntity>() {
            @Override
            public StatusEntity jsonToObj(String responseStr) {
                return new Gson().fromJson(responseStr, StatusEntity.class);
            }

            @Override
            public void onFail(int failCode, Object arg, Object tag) {
                hideProgressDialog();
            }

            @Override
            public void onResponseError(int failCode, Object arg, Object tag) {
                hideProgressDialog();
            }

            @Override
            public void onStart(Object tag) {
                showProgressDialog();
            }

            @Override
            public void onResponse(ResponseEntity<StatusEntity> response, Object tag) {
                hideProgressDialog();
                if (response != null && response.getResponseElement() != null && response.getResponseElement().isSuccess()) {
                    Intent intent = new Intent();
                    intent.putExtra("managerName", managerName);
                    intent.putExtra("managerPhone", managerPhone);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });

    }


}
